#include "tool/Proto.h"
#include "lib_json/json.h"
#include <fstream>
#include <iostream>


// 10000 任务信息
std::string Proto::Pack_10000(std::shared_ptr<CameraInfo>& pCamera){
    Json::Value jroot;
    const int& id = pCamera->id;
    const std::vector<Task>& tasks = pCamera->tasks;
    const std::map<int, std::vector<Roi>>& rois = pCamera->rois;
    jroot["code"] = 10000;
    
    Json::Value jtasks;
    // Tasks
    for (size_t i = 0; i < tasks.size(); i++)
    {
        const Task& _task = tasks[i];
        Json::Value jtask;
        jtask["cate"] = _task.cate;
        jtask["id"] = _task.id;
        jtask["name"] = _task.name;
        jtask["type"] = _task.type;
        Json::Value jextras;
        for (size_t j = 0; j < _task.extras.size(); j++)
        {
            jextras.append(_task.extras[j]);
        }
        jtask["extras"] = jextras;

        // 塞入ROI 这边再做一次安全判断!!!
        auto it = rois.find(_task.type);
        if (it == rois.end()) continue;
        
        Json::Value jrois;
        // Roi
        for (size_t i = 0; i < it->second.size(); i++)
        {
            const Roi& _roi = it->second[i];
            Json::Value jroi;
            for (size_t j = 0; j < _roi.points.size(); j++)
            {
                const RoiPoint& _point = _roi.points[j];
                Json::Value jpoint;
                jpoint["x"] = _point.x;
                jpoint["y"] = _point.y;
                jroi.append(jpoint);
            }
            jrois.append(jroi);
        }   
        jtask["rois"] = jrois;

        jtasks.append(jtask);
    }

    Json::Value jdata;
    jdata["id"] = id;
    jdata["task"] = jtasks;
    
    jroot["data"] = jdata;

    return jroot.toStyledString();
}

std::string Proto::Pack_10010(const int& id, const std::string& path){
    Json::Value jroot;
     
    jroot["code"] = 10010;

    Json::Value jdata;
    jdata["id"] = id;
    jdata["path"] = path;
    
    jroot["data"] = jdata;

    return jroot.toStyledString();
}